﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Dynamic;
using System.Transactions;
using PagedList;
using Sovigaz.DataProvider;
using Utilities;

namespace Sovigaz.Permission.Repositories
{
    public class DeliveryPlanLineRepository : GenericRepository
    {
        public DeliveryPlanLineRepository()
            : base("DeliveryPlanLineID")
        {
        }

        public ICollection<DeliveryPlanLine> DeliveryPlanLines { get; set; }

        public void Delete(int id)
        {
            var item = GetByID(id);
            if (item != null)
                item.IsDeleted = true;

            _context.SaveChanges();
        }

        public void DeleteByDeliveryPlanID(int id, bool isDeleted)
        {
            List<DeliveryPlanLine> list = GetByDeliveryPlanID(id) as List<DeliveryPlanLine>;
            bool flag;
            var orderrepository = new OrderRepository();   
            var orderlinerepository = new OrderLineRepository();
            foreach (var item in list)
            {
                flag = false;
                List<OrderLine> listorderline = orderlinerepository.GetList(orderlinerepository.GetByID(item.OrderLineID).OrderID);
                
                if (isDeleted) _context.DeliveryPlanLines.Remove(item);
                else item.IsDeleted = true;
                _context.SaveChanges();

                foreach (var orderline in listorderline)
                    if (_context.DeliveryPlanLines.Count(d => d.OrderLineID == orderline.OrderLineID && !d.IsDeleted) > 0) flag = true;
                if (!flag) orderrepository.UpdateStatus(orderlinerepository.GetByID(item.OrderLineID).OrderID, "N");
            }
            _context.SaveChanges();
        }

        public void Delete(string[] idArr)
        {
            foreach (var id in idArr)
            {
                var item = GetByID(ConvertUtility.IntParse(id));
                if (item != null)
                    item.IsDeleted = true;
            }

            _context.SaveChanges();
        }

        public void Delete(DeliveryPlan deliveryPlan)
        {
            foreach (var deliveryPlanLine in deliveryPlan.DeliveryPlanLines)
            {
                var item = GetByID(ConvertUtility.IntParse(deliveryPlanLine.DeliveryPlanLineID));
                if (item != null)
                    item.IsDeleted = true;
            }

            _context.SaveChanges();
        }

        public int Update(DeliveryPlanLine deliveryPlan)
        {
            var update = new DeliveryPlanLine();

            using (var scope = new TransactionScope())
            {
                if (deliveryPlan.DeliveryPlanLineID > 0)
                {
                    update = GetByID(deliveryPlan.DeliveryPlanLineID);
                    if (!Utility.ArrayEqual(update.Tstamp, deliveryPlan.Tstamp))
                        throw ExceptionHelper.CreateHttpResponseException("Dữ liệu đã bị cập nhật bởi người sử dụng khác.");
                }
                update.DeliveryPlanLineID = deliveryPlan.DeliveryPlanLineID;
                update.Description = deliveryPlan.Description;

                if (update.DeliveryPlanLineID == 0)
                    _context.DeliveryPlanLines.Add(update);

                _context.SaveChanges();

                scope.Complete();
            }

            _context.Entry(update).Reload();

            return update.DeliveryPlanLineID;
        }

        public DeliveryPlanLine GetByID(int id)
        {
            return _context.DeliveryPlanLines.FirstOrDefault(p => p.DeliveryPlanLineID == id && !p.IsDeleted);
        }

        public ICollection<DeliveryPlanLine> GetByDeliveryPlanID(int id)
        {
            return _context.DeliveryPlanLines.Where(p => p.DeliveryPlanID == id && !p.IsDeleted).ToList();
        }

        public DeliveryPlanLineRepository GetList(string whereClause, string orderBy, string orderDirection, int pageNumber)
        {
            var deliveryPlans = _context.DeliveryPlanLines.Where(whereClause)
                .OrderBy(String.Format("{0} {1}", orderBy.Trim(), orderDirection.Trim()))
                .ToPagedList(pageNumber, DefaultPageSize);

            TotalItemCount = deliveryPlans.TotalItemCount;
            TotalItemOnPage = deliveryPlans.Count;
            PageCount = deliveryPlans.PageCount;
            PageNumber = deliveryPlans.PageNumber;
            DeliveryPlanLines = deliveryPlans.ToList();

            return this;
        }

        public List<DeliveryPlanLine> GetList(int? deliveryPlanID)
        {
            if (deliveryPlanID.HasValue && deliveryPlanID > 0)
                return _context.DeliveryPlanLines.Where(p => !p.IsDeleted && p.DeliveryPlanID == deliveryPlanID).ToList();
            else return new List<DeliveryPlanLine>();
        }

        public void Update(ICollection<DeliveryPlanLine> deliveryPlanLines, int deliveryPlanID, TransactionScope transactionScope)
        {
            foreach (DeliveryPlanLine deliverPlanLine in deliveryPlanLines)
            {
                var update = new DeliveryPlanLine();
                deliverPlanLine.DeliveryPlanID = deliveryPlanID;
                if (deliverPlanLine.DeliveryPlanLineID > 0)
                    update = GetByID(deliverPlanLine.DeliveryPlanLineID);
                update.DeliveryPlanID = deliveryPlanID;
                update.OrderLineID = deliverPlanLine.OrderLineID;
                update.PlanQty = deliverPlanLine.PlanQty;
                if (update.DeliveryPlanLineID <= 0)
                    _context.DeliveryPlanLines.Add(update);

                var repository = new OrderLineRepository();
                var orderrepository = new OrderRepository();
                OrderLine orderline = repository.GetByID(update.OrderLineID);
                orderrepository.UpdateStatus(orderline.OrderID, "I");
            }
            _context.SaveChanges();
        }

    }
}
